
<template>
  <el-dialog :close-on-click-modal="false"
             title="编辑采购合同"
             v-el-drag-dialog
             :visible.sync="formVisible"
             center
             width="60%"
             :before-close="handleClose"
             >

      <form-assembly
        ref="formAssembly"
        :entryFrom="entryFrom"
        :formData="dynFormData.formData"
        :mainSubTable="dynFormData.mainSubTable"
        @makeSure="doUpdate"
        @cancel="closeDialog">
      </form-assembly>

  </el-dialog>
</template>


<script>
import purchaseContractApi from '@/api/platformLiyonghui/purchaseContract'
import FormAssembly from '@/components/FormAssembly'
import specialAttr from '@/utils/special-attr'
import principalPartApi from '@/api/platformLiyonghui/principalPart'
import { mapGetters } from 'vuex'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'PurchaseContractEdit',
  directives: { elDragDialog },
  components: {
    FormAssembly
  },
  data() {
    return {
      parentId: '0',
      // 动态表单
      dynFormData:{
        formData:{},
      },
      entryFrom: 'edit',//入口，add(新增),edit(编辑)，判断是从‘新增’进入，还是从‘编辑’进入，必填
      editId: null,//编辑id值
      subTable: {},
options: {


        principalPart: []

 },
      formVisible: false,
    }
  },
  computed: {
    ...mapGetters([
      'dictList'
    ])
  },
   async created() {
      principalPartApi.findOptions({sortBy: 'id',sortOrder: 'desc',q: '',}).then(res => {
        this.options.principalPart = res.data.map(entity => ({label: entity.name, value: entity.id}))
      })
  },
  methods: {
    /**
     * 打开编辑表单
     */
    handleUpdate(rowItem ) {
      // 处理一对一
      let row;
      if (rowItem) {
        row = {};
        for (let item in rowItem) {
          let itemValue = rowItem[item]
          if (itemValue.constructor === Object) {
            for (let childItem in itemValue) {
                row[item + '.' + childItem] = itemValue[childItem]
            }
          }else {
            row[item] = rowItem[item];
          }
        }
      }
      if(row){
        this.parentId = row.parentId
      } else {
        this.parentId = '0'
      }
      // const objData = JSON.parse(JSON.stringify(this.$store.getters.dynamicList[[this.$route.meta.id]][0]));
      const objDatas = [];
      const code = sessionStorage.getItem('routerCode');
      const menus = JSON.parse(sessionStorage.getItem('routerMenus'));
      console.log(code)
      console.log(menus)
      if(menus!=null){
        menus.forEach((item) => {
          if(item.menuCode==code){
            objDatas.push(item)
          }
        })
      }
      const objData = objDatas[0]
      //获取字典项
      const dictionaryEntry = JSON.parse(sessionStorage.getItem('constCode'))
      objData.formData.formList.forEach(item => {
        item.prop = item.prop.replace('.', '$')
        // 组装枚举数据
        if(item.fieldSpecialType === specialAttr.fieldSpecialType.ENUM_TYPE.code){
          // item.options = this.$store.getters.dictList[item.dicTypeData].map(item => ({ label: item.name, value: item.value}))
          item.options = []
          dictionaryEntry.forEach((item1) => {
            if(item1.constCode == item.dicTypeData){
              item.options.push({ label: item1.name, value: item1.value })
            }
          })
        // 组装外键数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.FK_TYPE.code){
          item.options = this.options[item.foreignData]
        // 组装多对多数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.MTM_TYPE.code){
          item.options = this[item.webEntityData]
        // 组装其他数据
        }else if(item.fieldSpecialType === specialAttr.fieldSpecialType.OTHER_WEB.code){
          item.options = this[item.webEntityData]
        }
        // 回显的value值
        if(row.hasOwnProperty(item.prop)){
          item.value = row[item.prop]
        }
        //编码规则校验
        if(item.isSerialNumber==true){
          item.disabled = true
          item.isCustomValue = true
        }
      })
      this.editId = row.id;
      this.dynFormData = objData
      this.formVisible = true
    },
    /**
     * 执行修改操作
     */
    doUpdate(form) {
      form.parentId = this.parentId;
      const args = Object.assign({id: this.editId}, form)
      purchaseContractApi.update(args)
        .then(res => {
          if (res.code === '000000') {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.$emit('updated', res.data)
            this.$refs['formAssembly'].cancelForm()
            this.formVisible = false
          }else{
             this.$message({
             type: 'error',
             message: res.message
            })
          }
        })
    },
    // 子组件点击取消按钮让dialog的formVisible:false;
    closeDialog(){
      this.formVisible = false;
    },
    // 点击右上角的关闭按钮时让form表单置空
    handleClose(done){
      this.$refs['formAssembly'].cancelForm();
      done();
    },
  }
}
</script>
